package com.huafv.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huafv.reggie.common.CustomException;
import com.huafv.reggie.entity.Category;
import com.huafv.reggie.entity.Dish;
import com.huafv.reggie.entity.Setmeal;
import com.huafv.reggie.mapper.CategoryMapper;
import com.huafv.reggie.service.CategoryService;
import com.huafv.reggie.service.DishService;
import com.huafv.reggie.service.SetmealService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class
CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService {

    @Resource
    private DishService dishService;

    @Resource
    private SetmealService setmealService;


    /**
     * 根据Id删除分类
     * @param id 分类Id
     */
    @Override
    public void remove(Long id) {
        // 查询分类是否已经关联菜品
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 添加查询条件
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int dishCount = dishService.count(dishLambdaQueryWrapper);
        if(dishCount > 0){
            throw new CustomException("当前分类下关联了菜品不能删除");
        }
        // 查询分类是否已经关联套餐
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        int setmealCount = setmealService.count(setmealLambdaQueryWrapper);
        if(setmealCount > 0){
            throw new CustomException("当前分类下关联了套餐不能删除");
        }

        super.removeById(id);
    }
}
